using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameFramework.Fsm;
using GameFramework.Event;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
using GameFramework.Localization;
using LS.Localization;

namespace LS.Procedure
{
    public class ProcedurePreloadDictionary : ProcedurePreloaderBase
    {
        public override bool UseNativeDialog => false;
        public override int PreloadPriority => 12;
        private Dictionary<string, bool> m_LoadedFlag = new Dictionary<string, bool>();

        protected override void OnEnter(IFsm<GameFramework.Procedure.IProcedureManager> procedureOwner)
        {
            base.OnEnter(procedureOwner);

            LSEntry.Event.Subscribe(LoadLocalizationSettingSuccessEventArgs.EventId, OnLoadDictionarySuccess);
            LSEntry.Event.Subscribe(LoadLocalizationSettingFailEventArgs.EventId, OnLoadDictionaryFailure);
            Log.Info("ProcedurePreloadDictionary Enter");
            m_LoadedFlag.Clear();
            
            PreloadResources();
        }

        protected override void OnUpdate(IFsm<GameFramework.Procedure.IProcedureManager> procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);

            foreach (KeyValuePair<string, bool> loadedFlag in m_LoadedFlag)
            {
                if (!loadedFlag.Value)
                {
                    return;
                }
            }
            ChangeNextProcedure(GetNextProcedureType());
        }

        protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
        {
            LSEntry.Event.Unsubscribe(LoadLocalizationSettingSuccessEventArgs.EventId, OnLoadDictionarySuccess);
            LSEntry.Event.Unsubscribe(LoadLocalizationSettingFailEventArgs.EventId, OnLoadDictionaryFailure);
            Log.Info("ProcedurePreloadDictionary Leave");
            base.OnLeave(procedureOwner, isShutdown);
        }

        private void PreloadResources()
        {
            InitLocalizationLoader();
        }

        private void InitLocalizationLoader()
        {
            Log.Info("ProcedurePreloadDictionary.InitLocalizationLoader");
            string settingAssetName = $"{LSPath.UnityLSRuntimeAssetsPath}/{LSPath.LSLocalizationDirectoryName}/LocalizationSetting{LSPath.UnityAssetExtension}";
            LSEntry.LocalizationLoader.defaultLanguage = Language.ChineseSimplified;
            LSEntry.LocalizationLoader.reloadDataWhenSetup = true;
            LSEntry.LocalizationLoader.saveLanguageTypeWhenSetup = true;
            LSEntry.LocalizationLoader.loadLanguageTypeWhenInit = true;
            Log.Info("ProcedurePreloadDictionary.InitLocalizationLoader----start init");
            LSEntry.LocalizationLoader.Init(settingAssetName,this);
        }

        private void OnLoadDictionarySuccess(object sender, GameEventArgs e)
        {
            LoadLocalizationSettingSuccessEventArgs ne = (LoadLocalizationSettingSuccessEventArgs)e;
            if (ne.userData != this)
            {
                return;
            }

            m_LoadedFlag[ne.assetName] = true;
            Log.Info("Load dictionary '{0}' OK.", ne.assetName);
        }

        private void OnLoadDictionaryFailure(object sender, GameEventArgs e)
        {
            LoadLocalizationSettingFailEventArgs ne = (LoadLocalizationSettingFailEventArgs)e;
            if (ne.userData != this)
            {
                return;
            }

            Log.Error("Can not load localization setting '{0}' with error message '{2}'.", ne.assetName, ne.errorMessage);
            m_LoadedFlag[ne.assetName] = true;
        }
    }
}
